package de.elmar_baumann.nb.slclock.stopwatch;

import de.elmar_baumann.nb.slclock.util.PropertyChangeNotifier;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import javax.swing.Icon;
import javax.swing.JFrame;
import org.netbeans.api.annotations.common.StaticResource;
import org.openide.util.ImageUtilities;
import org.openide.util.NbBundle;
import org.openide.util.NbPreferences;
import org.openide.windows.WindowManager;

/**
 * @author Elmar Baumann
 */
public class StopwatchDisplayPanel extends javax.swing.JPanel {

    public static final String KEY_SHOW_ICON = "StopwatchDisplayPanel.ShowIcon";
    @StaticResource private static final String ICON_PATH_STOPWATCH_RUNS = "de/elmar_baumann/nb/slclock/icons/stopwatch-runs.png";
    @StaticResource private static final String ICON_PATH_STOPWATCH_PAUSES = "de/elmar_baumann/nb/slclock/icons/stopwatch-pauses.png";
    private static final Icon ICON_STOPWATCH_RUNS = ImageUtilities.loadImageIcon(ICON_PATH_STOPWATCH_RUNS, false);
    private static final Icon ICON_STOPWATCH_PAUSES = ImageUtilities.loadImageIcon(ICON_PATH_STOPWATCH_PAUSES, false);
    private static final long serialVersionUID = 1L;
    private final StopwatchPanel panelStopwatch = new StopwatchPanel();
    private final JFrame frameStopwatch = new JFrame();

    public StopwatchDisplayPanel() {
        initComponents();
        initFrame();
        panelStopwatch.addPropertyChangeListener(StopwatchPanel.PROPERTY_RUN, stopwatchRunListener);
        addMouseListener(displayListener);
        labelIcon.addMouseListener(displayListener);
        setVisible(NbPreferences.forModule(StopwatchDisplayPanel.class).getBoolean(KEY_SHOW_ICON, true));
        PropertyChangeNotifier.INSTANCE.addPropertyChangeListener(showIconListener);
    }

    private void initFrame() {
        frameStopwatch.setTitle(NbBundle.getMessage(StopwatchDisplayPanel.class, "StopwatchDisplayPanel.Frame.Title"));
        frameStopwatch.setIconImage(WindowManager.getDefault().getMainWindow().getIconImage());
        frameStopwatch.add(panelStopwatch);
        frameStopwatch.setLocationRelativeTo(WindowManager.getDefault().getMainWindow());
        frameStopwatch.pack();
    }

    private final MouseListener displayListener = new MouseAdapter() {
        @Override
        public void mousePressed(MouseEvent e) {
            if (e.getButton() == MouseEvent.BUTTON1) {
                if (frameStopwatch.isVisible()) {
                    frameStopwatch.toFront();
                } else {
                    frameStopwatch.setVisible(true);
                }
            }
        }
    };

    private final PropertyChangeListener stopwatchRunListener = new PropertyChangeListener() {
        @Override
        public void propertyChange(PropertyChangeEvent evt) {
            if (StopwatchPanel.PROPERTY_RUN.equals(evt.getPropertyName())) {
                boolean run = (boolean) evt.getNewValue();
                labelIcon.setIcon(run ? ICON_STOPWATCH_RUNS : ICON_STOPWATCH_PAUSES);
            }
        }
    };

    private final PropertyChangeListener showIconListener = new PropertyChangeListener() {
        @Override
        public void propertyChange(PropertyChangeEvent evt) {
            if (PropertyChangeNotifier.PROPERTY_SHOW_STOPWATCH_ICON.equals(evt.getPropertyName())) {
                boolean show = (boolean) evt.getNewValue();
                NbPreferences.forModule(StopwatchDisplayPanel.class).putBoolean(KEY_SHOW_ICON, show);
                setVisible(show);
            }
        }
    };

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    private void initComponents() {//GEN-BEGIN:initComponents

        labelIcon = new javax.swing.JLabel();

        setLayout(new java.awt.GridBagLayout());

        labelIcon.setIcon(new javax.swing.ImageIcon(getClass().getResource("/de/elmar_baumann/nb/slclock/icons/stopwatch-pauses.png"))); // NOI18N
        add(labelIcon, new java.awt.GridBagConstraints());
    }//GEN-END:initComponents


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel labelIcon;
    // End of variables declaration//GEN-END:variables

}
